﻿#include "CAlgorthmSensor.h"
#include "CHeader.h"
#include "CLogOper.h"
//#include "IFAlarm.h"
CAlgorthmSensor::CAlgorthmSensor(QString sSceneId)
{
}

CAlgorthmSensor::~CAlgorthmSensor()
{
}

bool CAlgorthmSensor::actionAlgorthm(SCPolItemCom * pPolItemCom, QString sTaskName)
{
	m_pRBasicData->m_pMutex->lock();
	CDevDataBase* pDevDataRob = (CDevDataBase*)m_pDev->m_pDevDataBase;
	CPolItemSensor *pPolItemSensor = (CPolItemSensor*)pPolItemCom->m_pPolItem;
	double dValueHH = pPolItemSensor->m_sDataThreadHoldHH.toDouble();
	double dValueH = pPolItemSensor->m_sDataThreadHoldH.toDouble();
	double dValueLL = pPolItemSensor->m_sDataThreadHoldLL.toDouble();
	double dValueL = pPolItemSensor->m_sDataThreadHoldL.toDouble();
	double dValue = 0;
	QString sDevName = "";
	QString sUnit = "";
	switch (pPolItemSensor->m_nDevTypeCode)//
	{
	case enPM25://PM2.5
	{
		dValue = pDevDataRob->m_sSensorPM25.toDouble();
		sDevName = A2T("PM2.5");
		sUnit = A2T("ug/m3");
	}
		break;
	case enPM10://PM10
	{
		dValue = pDevDataRob->m_sSensorPM10.toDouble();
		sDevName = A2T("PM10");
		sUnit = A2T("ug/m3");
	}
		break;
	case enTemp://温度
	{
		dValue = pDevDataRob->m_sSensorTemp.toDouble();
		sDevName = A2T("温度");
		sUnit = A2T("℃");
	}
		break;
	case enGas://可燃气体
	{
		dValue = pDevDataRob->m_sSensorGas.toDouble();
		sDevName = A2T("可燃气体");
		sUnit = A2T("ppm");
	}
		break;
	case enCO://一氧化碳
	{
		dValue = pDevDataRob->m_sSensorCO.toDouble();
		sDevName = A2T("一氧化碳");
		sUnit = A2T("ppm");
	}
		break;
	case enHum://湿度
	{
		dValue = pDevDataRob->m_sSensorHum.toDouble();
		sDevName = A2T("湿度");
		sUnit = A2T("%RH");
	}
		break;
	default:
		dValue = pDevDataRob->m_sSensorTemp.toDouble();
		break;
	}

	CItemResultSensor *pItemResultSensor = (CItemResultSensor *)pPolItemCom->m_ItemResult;
	pItemResultSensor->m_dValue = dValue;
	double dCurrLocation = pDevDataRob->m_sRobCurrLoca.toFloat() / 1000;
	QString sRollerDesc = "";   //托辊名称
	int nRollerNum = 0;         //托辊序号
	nRollerNum = pDevDataRob->getCurrRollerNo(sRollerDesc);//连续检测结果的当前所在托辊号
	pItemResultSensor->m_dRealLocation = dCurrLocation;//连续检测结果的当前所在位置m
	pItemResultSensor->m_sRollerDesc = sRollerDesc;
	pItemResultSensor->m_nRollerNum = nRollerNum;
	pItemResultSensor->m_nBelong = pItemResultSensor->m_nBelong;
	pPolItemCom->m_pPolPoint->m_sRollerDesc = sRollerDesc;
	pPolItemCom->m_pPolPoint->m_nRollerCode = nRollerNum;
	pItemResultSensor->m_dActionTime = QDateTime::currentDateTime();
	if (dValue <= dValueLL)//  Val <= 低低限
	{
		pItemResultSensor->m_nState = -2;
		pItemResultSensor->m_nAlarmLevel = 2;
		pItemResultSensor->m_sDataDesc = A2T("%1,%2托辊,%3低低报警,报警值%4")
			.arg(pDevDataRob->m_sRobName)
			.arg(sRollerDesc)
			.arg(sDevName)
			.arg(QString::number(dValue, 'f', 1));
		pItemResultSensor->m_sResultDesc = A2T("%1低低报警,报警值%2")
			.arg(sDevName)
			.arg(QString::number(dValue, 'f', 1));
	}
	else if (dValueLL < dValue && dValue <= dValueL)//  低低限 < Val <= 低限
	{
		pItemResultSensor->m_sDataDesc = A2T("%1,%2托辊位置,%3低报警,报警值%4")
			.arg(pDevDataRob->m_sRobName)
			.arg(sRollerDesc)
			.arg(sDevName)
			.arg(QString::number(dValue, 'f', 1));
		pItemResultSensor->m_nState = -1;
		pItemResultSensor->m_nAlarmLevel = 1;
		pItemResultSensor->m_sResultDesc = A2T("%1低报警,报警值%2")
			.arg(sDevName)
			.arg(QString::number(dValue, 'f', 1));
	}
	else if (dValueL < dValue && dValue <= dValueH)//  低限 < Val <= 高限  (这个代表正常)
	{
		pItemResultSensor->m_nState = 0;
		pItemResultSensor->m_nAlarmLevel = 0;
		pItemResultSensor->m_sDataDesc = A2T("%1,%2托辊位置,进行%3检测,未报警,检测值:%4")
			.arg(pDevDataRob->m_sRobName)
			.arg(sRollerDesc)
			.arg(sDevName)
			.arg(QString::number(dValue, 'f', 1));
		pItemResultSensor->m_sResultDesc = A2T("进行%1检测,未报警,检测值:%2")
			.arg(sDevName)
			.arg(QString::number(dValue, 'f', 1));
	}
	else if (dValueH < dValue && dValue <= dValueHH)//  高限 < Val <= 高高限
	{
		pItemResultSensor->m_nState = 1;
		pItemResultSensor->m_nAlarmLevel = 1;
		pItemResultSensor->m_sDataDesc = A2T("%1,%2托辊,%3高报警,报警值%4")
			.arg(pDevDataRob->m_sRobName)
			.arg(sRollerDesc)
			.arg(sDevName)
			.arg(QString::number(dValue, 'f', 1));
		pItemResultSensor->m_sResultDesc = A2T("%1高报警,报警值%2")
			.arg(sDevName)
			.arg(QString::number(dValue, 'f', 1));
	}
	else//  高高限 < Val
	{
		pItemResultSensor->m_nState = 2;
		pItemResultSensor->m_nAlarmLevel = 2;
		pItemResultSensor->m_sDataDesc = A2T("%1,%2托辊,%3高高报警,报警值%4")
			.arg(pDevDataRob->m_sRobName)
			.arg(sRollerDesc)
			.arg(sDevName)
			.arg(QString::number(dValue, 'f', 1));
		pItemResultSensor->m_sResultDesc = A2T("%1高高报警,报警值%2")
			.arg(sDevName)
			.arg(QString::number(dValue, 'f', 1));
	}

	if (pItemResultSensor->m_nAlarmLevel > 0)
	{
		m_pRBasicData->m_pTaskExecInfo->m_nAlarmItemCount++;
		//发生报警要报警停留
		if (m_pRBasicData->m_nSmallSceneType == 1)//南电没有报警停留
		{
			SCtrInfo info;
			info.m_bCtrSync = true;
			info.m_sGuid = m_sGuid;
			info.m_bHasCtr = false;
			info.m_nCtrDevType = enOntology;
			info.m_nCtrCmd = ECCmdType::enCAlarmRemain;
			m_pDev->ctrDev(info);
		}
		//CSqlOperRob::instance().updateAlmInform(m_sSceneId, pPolItemSensor->m_nDevTypeCode); //更新报警通知表
	}
	/**
	* @ Author: Gaojia
	* @ Description: 更新报警牌数据
	*/
	/*SUpAlarmTechInfo cUpAlarmTechInfo;
	cUpAlarmTechInfo.sVal = QString::number(dValue);
	cUpAlarmTechInfo.sPersonName = A2T("");
	cUpAlarmTechInfo.sCorreId = pItemResultSensor->m_sItemGuid;
	cUpAlarmTechInfo.sRecordGuid = pItemResultSensor->m_sItemResultGuid;
	cUpAlarmTechInfo.sCorreInfo = "";
	cUpAlarmTechInfo.nAlarmLevel = pItemResultSensor->m_nAlarmLevel;
	cUpAlarmTechInfo.sDesc = pItemResultSensor->m_sDataDesc;
	if (pItemResultSensor->m_nAlarmLevel == 0)
	{
		cUpAlarmTechInfo.nAlarmState = 0;
	}
	else
	{
		cUpAlarmTechInfo.nAlarmState = 1;
	}
	cUpAlarmTechInfo.sResultDesc = pItemResultSensor->m_sResultDesc;*/
	m_pRBasicData->m_pMutex->unlock();
	//如果配置了巡检项关联检测设备
	//UpDetecState(cUpAlarmTechInfo);
	return true;
}
